package com.xjh.him.fragment.emotion.unicode;

import android.os.Build;
import android.util.ArrayMap;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;

import com.xjh.him.R;
import com.xjh.him.adapter.emotion.unicode.UnicodeEmotionAdapter;
import com.xjh.him.fragment.emotion.BaseEmotionPanelFragment;
import com.xjh.him.utils.EmotionUtils;
import com.xjh.him.utils.XToastUtils;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xui.utils.DensityUtils;

import java.util.ArrayList;

@Page(name = "unicode表情面板")
public class UnicodeEmotionPanelFragment extends BaseEmotionPanelFragment {

    @Override
    protected void initViews() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            ArrayMap<String, Integer> unicodeEmojiMap = EmotionUtils.getEmojiMap(EmotionUtils.EMOTION_UNICODE_TYPE);
            int rowNum = (int)Math.ceil((double) unicodeEmojiMap.size() / EmotionUtils.DEFAULT_COLUMN_NUM);
            int[] args = EmotionUtils.getGridArgs(getActivity(), rowNum);
            int itemWidth = args[0],
                    gvHeight = args[1],
                    gvWidth = DensityUtils.getDisplayMetrics().widthPixels,
                    padding = EmotionUtils.getEmotionPadding(getActivity());

            GridView gv = super.initGridViewParams(padding, gvWidth, gvHeight);
            ArrayList<String> emotionKeys = new ArrayList<>(unicodeEmojiMap.keySet());
            UnicodeEmotionAdapter adapter = new UnicodeEmotionAdapter(
                    getActivity(),
                    emotionKeys,
                    itemWidth);
            gv.setAdapter(adapter);
            gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    EditText editText = requireActivity().findViewById(R.id.input_msg);
                    int unicode = Integer.parseInt(emotionKeys.get(position));
                    editText.append(EmotionUtils.parseEmojiByUnicode(unicode));
                }
            });
        }else {
            XToastUtils.toast("build sdk version must greater 19, current is " + Build.VERSION.SDK_INT);
        }
    }
}
